import {
  Body,
  Controller,
  Delete,
  Get,
  Logger,
  Param,
  Post,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { createWriteStream } from 'fs';
import sharp from 'sharp';
import { diskStorage } from 'multer';
import { extname } from 'path';
import dayjs from 'dayjs';
import { ShaderService } from './shader.service';
import { ReqType } from 'src/decorator/reqtype.decorator';
import { AuthItem, Code } from 'src/global-type';
import { AuthInterceptor } from 'src/interceptor/auth.interceptor';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { QueryParams, createShaderType, updateShaderType } from './shader.dto';
import { OpenApiService } from 'src/open-api/open-api.service';
const formattedDate = dayjs().format('YYYY-MM-DD');

const MODULETYPE = 'shader';
@Controller('api/shader')
@ApiTags('渲染器展示')
export class ShaderController {
  private readonly logger = new Logger(ShaderController.name);
  constructor(
    private readonly shaderService: ShaderService,
    // private readonly openApiService: OpenApiService
  ) { }

  @Get('config/list')
  @ReqType(MODULETYPE, AuthItem.READ)
  @UseInterceptors(AuthInterceptor)
  @ApiOperation({ summary: '获取渲染器列表' })
  async queryShaderList(@Query() params: QueryParams) {
    try {
      const res = await this.shaderService.queryShaderList({
        ...params,
      });
      return {
        code: Code.OK,
        message: 'success',
        data: res,
      };
    } catch (err) {
      this.logger.error(err);
      return {
        code: Code.UNKNOW_ERROR,
        message: String(err),
        data: {
          total: 0,
          data: [],
        },
      };
    }
  }

  @Get('config/list/:id')
  @ReqType(MODULETYPE, AuthItem.READ)
  @UseInterceptors(AuthInterceptor)
  @ApiOperation({ summary: '查询单个渲染器信息' })
  async queryOneShader(
    @Param('id') id: string,
    // @OperUser() operUser: SystemUserDecoratorType,
  ) {
    const res = await this.shaderService.queryOneShader(id);

    if (!res)
      return {
        code: Code.UNKNOW_ERROR,
        message: '创建失败',
        data: null,
      };
    return {
      code: Code.OK,
      message: 'success',
      data: res,
    };
  }

  @Post('config/addList')
  @ReqType(MODULETYPE, AuthItem.CREATE)
  @UseInterceptors(AuthInterceptor)
  @ApiOperation({ summary: '添加渲染器信息' })
  async createShaderList(
    @Body() shaderDate: createShaderType,
    // @OperUser() operUser: SystemUserDecoratorType,
  ) {
    const res = await this.shaderService.createOneShader(shaderDate);
    if (res == 'repetition') {
      return {
        code: Code.FORMAT_ERROR,
        message: '物流名称重复',
        Date: null,
      };
    }
    if (!res) {
      return {
        code: Code.UNKNOW_ERROR,
        message: '创建失败',
        data: null,
      };
    }

    return {
      code: Code.OK,
      message: 'success',
      data: null,
    };
  }

  @Post('config/updateList')
  @ReqType(MODULETYPE, AuthItem.MODIFY)
  @UseInterceptors(AuthInterceptor)
  @ApiOperation({ summary: '修改渲染器信息' })
  async updateShaderList(@Body() shaderDate: updateShaderType) {
    const res = await this.shaderService.updateShader(shaderDate);
    if (res == 'repetition') {
      return {
        code: Code.FORMAT_ERROR,
        message: '物流名称重复',
        Date: null,
      };
    }
    if (!res) {
      return {
        code: Code.UNKNOW_ERROR,
        message: '创建失败',
        data: null,
      };
    }

    return {
      code: Code.OK,
      message: 'success',
      data: null,
    };
  }
  @Delete('config/delete/:id')
  @ReqType(MODULETYPE, AuthItem.DELETE)
  @UseInterceptors(AuthInterceptor)
  @ApiOperation({ summary: '删除渲染器信息' })
  async deleteShaderList(@Param('id') id: string) {
    const res = await this.shaderService.deleteShader(id);
    if (res == 'repetition') {
      return {
        code: Code.FORMAT_ERROR,
        message:
          '配置正在被使用，请删除地区配置中所有使用此配置的信息后方可删除',
        date: false,
      };
    } else {
      return true;
    }
  }

  // @Get('config/Options')
  // @ReqType(MODULETYPE, AuthItem.READ)
  // @UseInterceptors(AuthInterceptor)
  // @ApiOperation({ summary: '获取物流选项详情' })
  // async getShaderOptions(@Query() params: optionsParams) {
  //   try {
  //     const res = await this.shaderService.shaderOptions(params);
  //     return {
  //       code: Code.OK,
  //       message: 'success',
  //       data: res.list,
  //       total: res.total,
  //     };
  //   } catch (err) {
  //     this.logger.error(err);
  //     return {
  //       code: Code.UNKNOW_ERROR,
  //       message: String(err),
  //       data: {
  //         total: 0,
  //         data: [],
  //       },
  //     };
  //   }
  // }
}
